package com.mercury.ec.module.air.service.demandorder;

import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandscheme.AirDemandSchemePageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandscheme.AirDemandSchemeSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandSchemeDO;
import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.common.util.object.BeanUtils;

import com.mercury.ec.module.air.dal.mysql.demandorder.AirDemandSchemeMapper;

import static com.mercury.ec.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.mercury.ec.module.air.enums.ErrorCodeConstants.*;

/**
 * 机票需求订单方案 Service 实现类
 *
 * @author hesx
 */
@Service
@Validated
public class AirDemandSchemeServiceImpl implements AirDemandSchemeService {

    @Resource
    private AirDemandSchemeMapper demandSchemeMapper;

    @Override
    public Long createDemandScheme(AirDemandSchemeSaveReqVO createReqVO) {
        // 插入
        AirDemandSchemeDO demandScheme = BeanUtils.toBean(createReqVO, AirDemandSchemeDO.class);
        demandSchemeMapper.insert(demandScheme);
        // 返回
        return demandScheme.getId();
    }

    @Override
    public void updateDemandScheme(AirDemandSchemeSaveReqVO updateReqVO) {
        // 校验存在
        validateDemandSchemeExists(updateReqVO.getId());
        // 更新
        AirDemandSchemeDO updateObj = BeanUtils.toBean(updateReqVO, AirDemandSchemeDO.class);
        demandSchemeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDemandScheme(Long id) {
        // 校验存在
        validateDemandSchemeExists(id);
        // 删除
        demandSchemeMapper.deleteById(id);
    }

    private void validateDemandSchemeExists(Long id) {
        if (demandSchemeMapper.selectById(id) == null) {
            throw exception(AIR_DEMAND_SCHEME_NOT_EXISTS);
        }
    }

    @Override
    public AirDemandSchemeDO getDemandScheme(Long id) {
        return demandSchemeMapper.selectById(id);
    }

    @Override
    public PageResult<AirDemandSchemeDO> getDemandSchemePage(AirDemandSchemePageReqVO pageReqVO) {
        return demandSchemeMapper.selectPage(pageReqVO);
    }

}
